package com.tgu.home_action.controller;

import com.tgu.home_action.pojo.*;
import com.tgu.home_action.pojo.annotation.ApiJsonObject;
import com.tgu.home_action.pojo.annotation.ApiJsonProperty;
import com.tgu.home_action.pojo.annotation.MyLog;
import com.tgu.home_action.service.FaceService;
import com.tgu.home_action.service.utils.AliYunService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

@Api(tags = "人脸识别")
@RestController
@RequestMapping("/face")
public class FaceController {
    @Autowired
    private AliYunService aliYunService;
    @Autowired
    private FaceService faceService;


    @ApiOperation("判断是否是老人")
    @PostMapping("/isOldMan")
    public ResponseData<Map<String, Object>> isOldMan(@ApiJsonObject(name = "IsOldMan", value = {
            @ApiJsonProperty(key = "imgUrl", description = "老人照片")
    }) @RequestBody Map<String, Object> param) {
        String imgUrl = (String) param.get("imgUrl");
        return aliYunService.isOldMan(imgUrl);
    }

    @MyLog(actionId = 11, role = "volunteer", type = 2)
    @ApiOperation("人脸识别")
    @PostMapping
    public ResponseData<Face> faceIdentification(@ApiJsonObject(name = "FaceIdentification", value = {
            @ApiJsonProperty(key = "imgUrl", description = "照片地址"),
    }) @RequestBody Map<String, Object> param, @ApiIgnore @ModelAttribute("volunteerUser") Volunteer volunteer) {
        String imgUrl = (String) param.get("imgUrl");
        return faceService.faceIdentification(imgUrl, volunteer);
    }

    @ApiOperation("查询人脸识别记录")
    @GetMapping
    public ResponseData<List<Face>> getFaceRecords(@RequestParam Integer oldManId) {
        return faceService.getFaceRecords(oldManId);
    }


    @ApiOperation("家属确认人脸识别结果,state为3是成功，4是失败")
    @PostMapping("/result")
    @PreAuthorize("hasRole('family')")
    public ResponseData<Object> confirmResult(@ApiJsonObject(name = "ConfirmResult", value = {
            @ApiJsonProperty(key = "faceId", description = "人脸记录ID"),
            @ApiJsonProperty(key = "state", description = "确认结果，3为成功，4为失败")
    }) @RequestBody Map<String, Object> param, @ApiIgnore @ModelAttribute("familyUser") Family family) {
        Integer faceId = (Integer) param.get("faceId");
        int state = (int) param.get("state");
        return faceService.confirmResult(faceId, (short) state, family);
    }

    @ApiOperation("管理员确认人脸识别结果,state为3是成功，4是失败")
    @PostMapping("/result/admin")
    @PreAuthorize("hasAnyRole('admin','superAdmin','districtAdmin')")
    public ResponseData<Object> adminConfirmResult(@ApiJsonObject(name = "AdminConfirmResult", value = {
            @ApiJsonProperty(key = "faceId", description = "人脸记录ID"),
            @ApiJsonProperty(key = "state", description = "确认结果，3为成功，4为失败")
    }) @RequestBody Map<String, Object> param) {
        Integer faceId = (Integer) param.get("faceId");
        int state = (int) param.get("state");
        return faceService.adminConfirmResult(faceId, (short) state);
    }

    @ApiOperation("管理员查询face(可通过volunteerId,oldManId筛选)")
    @PreAuthorize("hasAnyRole('admin','superAdmin','districtAdmin')")
    @GetMapping("/admin")
    public ResponseData<Page<Face>> adminGetFaces(Face face) {
        return faceService.adminGetFaces(face);
    }

}
